<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>发送短信</title>
    <meta name="renderer" content="webkit">
    <link rel="shortcut icon" href="../img/favicon.ico">
    <link rel="Bookmark" href="../img/favicon.ico"/>
    <script src="../js/boot.js" type="text/javascript"></script>
</head>
<body>
    <div class="mini-fit2">
        <div class="bigdiv">
            <div class="bottontitle">
                <span>基本信息</span>
            </div>
            <table class="HZXXGLContent">
                <tr>
                    <td class="tabletitle">接收人：</td>
                    <td><input id="sendeeName" class="mini-textbox" /></td>
                </tr>
                <tr>
                    <td class="tabletitle">模板：</td>
                    <td>
                        <input id="infoTemplate" class="mini-combobox" textField="tmplContent" valueField="tmplContent" emptyText="不使用短信模板" onvaluechanged="CqzdSmsApp.selectTemplate(e)"
                               url="/cqzd/sysconfig/sms/queryList?pageSize=100" value=""  required="false" allowInput="false" showNullItem="true" nullItemText="不使用短信模板"/>
                    </td>
                </tr>
                <tr>
                    <td class="tabletitle">内容：</td>
                    <td><textarea id="infoContent" class="mini-textarea" style="resize:none;height: 200px;width: 100%;background-color: #fffded;"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <a class="mini-button mini-btn mini-btn-ok" id="btn-send">发 送</a>
                    </td>
                </tr>

            </table>
        </div>
        <div class="mindiv">
            <div>
                <input type="text" data-search="search" id="search-input" onclick2="showList(event,this)" />
            </div>
            <div id="recent-sms-list" class="LSXXbigdiv">

            </div>
        </div>
        <div class="mindiv2">
            <ul id="tree1" expandOnLoad="true" class="mini-tree" style="width:300px;padding:5px;" showTreeIcon="true"
                resultAsTree="false" textField="text" idField="id"  expandOnNodeClick="false" showCheckBox='true'
                onnodecheck="CqzdSmsApp.selectEmp(e)" checkOnTextClick="true"
            >
            </ul>
        </div>

        <div class="LSXXbigdiv" id="search-sms-list" data-search="search" style="position:absolute;z-index: 99;display: none;width: 280px;padding: 0.5%;margin-top: 0px;">
        </div>
    </div>
<!-- 短信记录模板 -->
<script id="sms-record-tmpl" type="text/x-jquery-tmpl">
     <div onclick="CqzdSmsApp.selectSms('{%= infoId%}')">
        <div class="sjlog">{%= index%}</div>
        <div>
            <p class="lsxxtitle">{%= infoContent%}</p>
            <span class="lsxxname">{%= senderName%}</span>
            <span class="lsxxtime">{%= sendTime%}</span>
        </div>
    </div>
</script>
<script type="text/javascript">
    /**
     * 短信记录对象
     */
    window.CqzdSmsApp = window.CqzdSmsApp || (function(){

        //短信内容输入框
        var infoContentInput;
        //人员树
        var tree;
        //接收人数组
        var sendeeArr = [];

        /**
         * 内容录入区域初始化
         */
        var contentInit = function(){
            //追加css样式引入，避免被浏览器缓存
            $("head").append("<link href=\"../css/SJDXFS.css?v="+ASSETS_VERSION+"\" rel=\"stylesheet\" type=\"text/css\" />");

            txkui.parse();
            //背景框自动撑满页面
            var fit2 = $(".mini-fit2");
            function fitDiv(){
                var h = $(window).height()-36;
                h = h < 476 ? 476 : h;
                fit2.css({height:h});
                var h2 = $(window).height()-290-22;
                h2 = h2 < 200 ? 200 : h2;
                $(".mindiv2").css({height:h2});
            }
            $(window).on("resize",fitDiv);
            fitDiv();

            infoContentInput = mini.get('infoContent');
            //发送按钮
            var btn = $("#btn-send");
            btn.on("click",function(){
                var data = {
                    infoContent:infoContentInput.getValue()
                    //,sendTime:new Date().format('yyyy-MM-dd hh:mm:ss')
                    ,smsSendee:sendeeArr
                };

                if(!data.smsSendee || !data.smsSendee.length){
                    return TipsUtil.toastErr('需选择接收人');
                }

                if(!data.infoContent || !data.infoContent.trim()){
                    return TipsUtil.toastErr('需填写短信内容');
                }

                console.log(sendeeArr);
                mini.confirm("确定发送短信吗？","",function(action){
                    if("ok" !== action){
                        return;
                    }
                    //开始发送
                    btn.attr("disabled","disabled");
                    HttpUtil.post(SVR_PATH+"cqzd/do/sms/send",JSON.stringify(data),function(e){
                        btn.removeAttr("disabled");
                        if(e.msg==null){
                            TipsUtil.toast("发送成功");
                        }else {
                            TipsUtil.toastErr(e.msg);
                        }
                        //重载发送记录来刷新页面区域
                        recentRecordReload();
                    },function () {
                        btn.removeAttr("disabled");
                    });
                });
            });
        };

        /**
         * 机构人员树初始化
         */
        var treeInit = function(){
            tree = mini.get("tree1");
            //无人员的部门已被后台自动过滤
            $.post(SVR_PATH+"cqzd/do/org/organization/listAllTreeNode/cqzd",null,function(e){
                tree.loadList(e.data, "id", "pid");
                tree.expandAll();
            });
        };

        /**
         * 历史短信查询初始化
         */
        var historiesInit = function(){
            //搜索的记录结果列表
            var searchSmsList = $("#search-sms-list");
            //最近发送记录的列表
            var recentSmsList = $("#recent-sms-list");
            var input = $("#search-input");
            //点击页V面中的其他区域，自动隐藏【搜索的记录结果列表】
            $("html").on("click", "body,div,input", function(e) {
                if (!$(e.currentTarget).data("search")) {
                    searchSmsList.hide();
                    input.val('');
                }
            });

            $("#list-data").on("click", function(e) {
                //e.preventDefault();
            });

            //搜索框的按键事件，提交搜索动作
            input.on("keyup", function(e) {

                loadSmsRecord(this.value,searchSmsList);
                //定位显示搜索列表div
                var offset = $(this).offset();
                searchSmsList.show().css({
                    top: offset.top + 40
                    ,left: offset.left
                    ,width: recentSmsList.width()+15
                });
            });
        };
        /**
         * 重载最近的短信记录
         */
        var recentRecordReload = function(){

            loadSmsRecord(null,$("#recent-sms-list"))
        };

        /**
         * 根据查询条件加载短信记录
         * @param infoContent 查询条件
         * @param listEl 用来填充结果的对象
         */
        var loadSmsRecord = function(infoContent,listEl){
            var smsListItemTmpl = $("#sms-record-tmpl");
            $.post(SVR_PATH+"cqzd/do/sms/list?pageSize=4",{infoContent:infoContent||null},function (e) {
                if(HttpUtil.hasErr(e, false))
                    return;
                var data  = e.data;
                if(!data.length){
                    listEl.html(infoContent ? '没有匹配的短信记录' : '没有短信记录');
                }else{
                    for (var i = 0; i < data.length; i++) {
                        data[i].index = i+1;
                        //显示短信内容的前18个字
                        if(data[i].infoContent.length>20){
                            data[i].infoContent = data[i].infoContent.substring(0,18)+'...';
                        }
                    }
                    listEl.html('');
                    //利用模板将短信显示到页面中
                    smsListItemTmpl.tmpl(data).appendTo(listEl);
                }
            });
        }

        /**
         * 选择短信接收人
         * 点击人员树调用
         * 数据回显调用
         *
         * @param emps 接收人节点数组
         */
        var setSendee = function(emps){
            var names = [];
            sendeeArr.length = 0;
            var ptext;
            for(var i = 0,len=emps.length ; i<len ; i++){
                if(emps[i].isLeaf){
                    //显示人员的组织名称，如果组织名称长度大于两位，则截取前两位
                    ptext = emps[i].ptext||"";
                    if(ptext.length>2){
                        ptext = ptext.substring(0,2);
                    }
                    if(ptext){
                        ptext = "["+ptext+"]";
                    }
                    names.push(emps[i].text + ptext);
                    //将人员加到sendeeArr中，提交时，从sendeeArr取1接收人
                    sendeeArr.push({
                        sendeeId:emps[i].realId
                        ,sendeeName:emps[i].text
                        ,branchId:emps[i].pid ? (emps[i].pid.substring(4)) : null
                        ,branchName:emps[i].ptext
                    });
                }
            }
            //在接收人输入框中，显示接收人信息
            mini.get('sendeeName').setValue(names.join(','));
        };

        /**
         * tree选择人员节点
         */
        var selectEmp = function(e){

            setSendee(tree.getCheckedNodes());
        };

        /**
         * tree选择短信内容模板
         */
        var selectTemplate = function(e){

            infoContentInput.setValue(e.value);

            var value = infoContentInput.getValue();
            if(!value){
            }
        };

        /**
         * 选择短信历史记录
         * 查询该ID对应的记录信息，并显示到左侧的短信form中
         * @param infoId 记录ID
         */
        var selectSms = function (infoId) {

            HttpUtil.get(SVR_PATH+"cqzd/do/sms/get/"+infoId,function (e) {
                infoContentInput.setValue(e.data.infoContent);
                var smsSendee = e.data.smsSendee;
                //从人员树中找出当前记录的接收人节点
                var nodes = tree.findNodes(function(node){
                    for (var i = 0; i < smsSendee.length; i++) {
                        if(node.id === "emp-"+smsSendee[i].sendeeId) return true;
                    }
                    return false;
                });
                //选中人员树种中的这些节点
                tree.uncheckAllNodes();
                tree.checkNodes(nodes);
                //把选择种的节点，显示到短信接收人输入框种去
                setSendee(nodes);
            });
        };

        return {
            init:function(){
                contentInit();
                treeInit();
                recentRecordReload();
                historiesInit();
            }
            ,selectEmp:selectEmp
            ,selectTemplate:selectTemplate
            ,selectSms:selectSms
        }
    })();

    $(function() {CqzdSmsApp.init()})
</script>
</body>
</html>